﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

///< 自定义脚本 >///
using Assets.Scripts;



public class TouchEventManger : MonoBehaviour
{

    public ESceneType CurrentScene = ESceneType.Start;
    public Stack<ESceneType> HistorVisitScene = new Stack<ESceneType>();

    public GameObject MenuRoot;
    public GameObject Logo;
    public GameObject MenuPanel;
    public GameObject CustomRoot;


    public GameObject StartButton;
    public GameObject SoundButton;
    public GameObject ExitButton;
    public GameObject FullButton;

    public GameObject[] Leds;
    private Vector2 oldSoundPos = Vector2.zero;


    public Sprite soundNormalSprite;
    public Sprite soundDisableSprite;
    [SerializeField] private bool defaultSoundState = true;


    private int CurrentFadeFlag = -1;
    private const int Fade_In = 1;
    private const int Fade_StartScene = 1 << 1;
    private const int Fade_MenuScene = 1 << 2;
    private const int Fade_CutomSelectScene = 1 << 3;
    private const int Fade_GameScene = 1 << 4;


    public CustomManager customMgr;

    private void Awake()
    {
        //记录声音按钮的原位置
        oldSoundPos = new Vector2(SoundButton.transform.position.x, SoundButton.transform.position.y);


        SoundButton.transform.position = new Vector2(ExitButton.transform.position.x, ExitButton.transform.position.y);
        ExitButton.SetActive(false);
        MenuPanel.SetActive(false);
        CustomRoot.SetActive(false);
    }

    void Start()
    {
        //所有展示LED 亮灯
        Shine();

        //推入历史访问场景堆栈记录
        HistorVisitScene.Push(ESceneType.Start);
    }


    /// <summary>
    /// 返回上一层
    /// </summary>
    public void onTouchReturnLast()
    {
        if (CurrentScene == ESceneType.Start) return;

        HistorVisitScene.Pop();
        CurrentScene = HistorVisitScene.Peek();
        CurrentFadeFlag = Fade_In | (int)CurrentScene;
        StartCoroutine("FadeInNewScene");
    }

    public void onTouchStart()
    {
        CurrentFadeFlag = Fade_In | Fade_MenuScene;
        StartCoroutine("FadeInNewScene");

        CurrentScene = ESceneType.Menu;
        HistorVisitScene.Push(CurrentScene);
    }

    IEnumerator FadeInNewScene()
    {

        ExitButton.SetActive(false);
        FullButton.SetActive(false);
        SoundButton.SetActive(false);
        MenuPanel.SetActive(false);
        CustomRoot.SetActive(false);

        //切换场景前 设置
        switch (CurrentFadeFlag)
        {

            case Fade_In | Fade_MenuScene:
                {
                    Logo.SetActive(false);
                    StartButton.SetActive(false);
                    HideLeds();
                }
                break;
            case Fade_In | Fade_CutomSelectScene:
                {
                    MenuPanel.SetActive(false);
                }
                break;
        }


        SpriteRenderer bg = MenuRoot.GetComponent<SpriteRenderer>();

        //淡出
        float tm = 0;
        while (true)
        {
            bg.color = Color.Lerp(bg.color, Color.black, Time.deltaTime);
            tm += Time.deltaTime;


            if (tm >= 0.999)
            {
                bg.color = Color.black;
                break;
            }

            yield return 0;
        }

        //场景过渡设置
        switch (CurrentFadeFlag)
        {

            case Fade_In | Fade_MenuScene:
                {
                    ToMenuScene();
                }
                break;
            case Fade_In | Fade_StartScene:
                {
                    BackStartScene();
                }
                break;
            case Fade_In | Fade_CutomSelectScene:
                {
                    CustomRoot.SetActive(true);
                }
                break;
        }

        //淡入
        tm = 0;
        while (true)
        {
            bg.color = Color.Lerp(bg.color, Color.white, Time.deltaTime);
            tm += Time.deltaTime;

            if (tm >= 0.999)
            {
                bg.color = Color.white;
                break;
            }

            yield return 0;
        }



        SoundButton.SetActive(true);
        ExitButton.SetActive(true);
        FullButton.SetActive(true);
    }

    /// <summary>
    /// 前往菜单界面时调用
    /// </summary>
    void ToMenuScene()
    {
        MenuPanel.SetActive(true);
        SoundButton.transform.position = new Vector3(oldSoundPos.x, oldSoundPos.y);
        
        StartCoroutine("MenuPanelTween");        
    }

    //在 按钮 动画播放后的一帧内 恢复 动画 show的bool值
    IEnumerator MenuPanelTween()
    {
        Animator temp = MenuPanel.GetComponent<Animator>();
        temp.SetBool("show", true);
        yield return 0;
        temp.SetBool("show", false);
    }


    //按钮位置恢复
    void ButtonPosReset()
    {
        SoundButton.transform.position = new Vector2(ExitButton.transform.position.x, ExitButton.transform.position.y);
        ExitButton.SetActive(false);

        StartCoroutine("ButtonTween");
    }


    void Shine()
    {
        Animator temp;
        foreach (GameObject led in Leds)
        {
            led.SetActive(true);
            temp = led.GetComponent<Animator>();
            temp.speed = Random.Range(1.2f, 2.1f);
            temp.SetBool("shine", true);
        }
        temp = null;
    }

    void HideLeds()
    {
        foreach (GameObject led in Leds)
        {
            led.SetActive(false);
        }

    }

    /// <summary>
    /// 回到开始界面时调用
    /// </summary>
    void BackStartScene()
    {
        if (StartButton == null) return;

        StartButton.SetActive(true);
        Logo.SetActive(true);

        //led动画
        Shine();

        //按钮位置恢复
        ButtonPosReset();
    }

    //在 按钮 动画播放后的一帧内 恢复 动画 show的bool值
    IEnumerator ButtonTween()
    {

        Animator temp = StartButton.GetComponent<Animator>();
        Animator temp2 = Logo.GetComponent<Animator>();

        temp2.SetBool("show", true);
        temp.SetBool("show", true);
        yield return 0;
        temp.SetBool("show", false);
        temp2.SetBool("show", false);
    }



    public void onTouchSound()
    {
        defaultSoundState = !defaultSoundState;

        Image texture = SoundButton.GetComponent<Image>();
        texture.sprite = defaultSoundState ? soundNormalSprite : soundDisableSprite;
    }


    public void onTouchDifficulty(int tag)
    {
        switch (tag)
        {
            case 0:
                {

                }
                break;
            case 1:
                {

                }
                break;
            case 2:
                {

                }
                break;
            case 3:
                {

                }
                break;
        }

        CurrentFadeFlag = Fade_In | Fade_CutomSelectScene;
        StartCoroutine("FadeInNewScene");
        
        CurrentScene = ESceneType.CustomSelect;
        HistorVisitScene.Push(CurrentScene);

        //切换关卡难度
        customMgr.chooseDifficulty = tag;
    }

}
